package com.android.lotto.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.text.TextWatcher;

import com.android.lotto.R;

public class PickerEditView extends LinearLayout {
	private EditText mPickerValue;
    private ImageView mPickerReduce;
    private ImageView mPickerAdd;
    private OnOperationListener mOnOperationListener= null;

	public PickerEditView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public PickerEditView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public PickerEditView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

    protected void onFinishInflate() {
        super.onFinishInflate();
        LayoutInflater.from(getContext()).inflate(R.layout.widget_picker_editview, this);
        init_widget();
        addListener();
        
    }
    
    public void init_widget(){
        mPickerValue = (EditText)findViewById(R.id.pickerValue);
        mPickerAdd = (ImageView)findViewById(R.id.pickerAdd);
        mPickerReduce = (ImageView)findViewById(R.id.pickerReduce);
        mPickerAdd.setBackgroundResource(R.drawable.add_minus_selector);
        mPickerReduce.setBackgroundResource(R.drawable.add_minus_selector);
        mPickerValue.setText("");
        mPickerValue.setEnabled(false);
    }
    
    public void addListener(){
        mPickerAdd.setOnClickListener(new OnClickListener() {
        	@Override
            public void onClick(View v) {
               if(mOnOperationListener != null){
            	   mOnOperationListener.onOperation(1);
               }
            }
        });
        
        mPickerReduce.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
            	if(mOnOperationListener != null){
             	   mOnOperationListener.onOperation(-1);
                }
            }
        });
    }
  
    public void setText(String  text){
    	if(text == null){
    		return;
    	}
    	mPickerValue.setText(text);
    }
    public String getText(){
        if(mPickerValue == null){
            return "";
        }
        return mPickerValue.getText().toString();
    }
    public void setTextColor(int color) {
        mPickerValue.setTextColor(color);
    }
    public void setOnOperationListener(OnOperationListener listener){
    	mOnOperationListener = listener;
    }
    public void setOnFocusChangeListener(OnFocusChangeListener l) {
        mPickerValue.setOnFocusChangeListener(l);
    }
    public void addTextChangedListener(TextWatcher l) {
        mPickerValue.addTextChangedListener(l);
    }
    public interface OnOperationListener{
    	void onOperation(int inc);
    }
    public void setInputType(int type) {
        mPickerValue.setInputType(type);
    }
    public void setEnabled(boolean enabled) {
        mPickerValue.setEnabled(enabled);
    }
}
